package com.woniu.money.controller;



import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.woniu.money.bean.TFinance;
import com.woniu.money.service.ITFinanceService;
import com.woniu.money.service.impl.TFinanceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/tFinance")
@CrossOrigin
public class TFinanceController {
    @Autowired
    private TFinanceServiceImpl fin;
    @Autowired
    private ITFinanceService ser;

    //应收账务,页面展示,分页,查询
    @GetMapping("/tt")
    public Page<TFinance> selectAll(
            Integer currentPage, Integer pageSize, Integer orderId,
            String username, LocalDateTime startTime, LocalDateTime endTime)
    {
        return fin.All(currentPage, pageSize, orderId, username, startTime, endTime);

    }


//    回单
    @GetMapping("ts")
    public List<TFinance> seltt( Integer orderId, String username){
        List<TFinance> seltt = fin.seltt(orderId, username);
        return seltt;
    }

    /**
     * 导出表格
     */
    @GetMapping("tss")
    public List<TFinance> sel(){
        List<TFinance> list = fin.sel();
        return list;
    }



    /**
     * 导出表格
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        // 从数据库查询出所有的数据
        List<TFinance> sel = fin.sel();
        // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");

        List<Map<String, Object>> rows = sel.stream().map(item -> {
            Map<String, Object> maps = new HashMap<>();
            maps.put("order_id", item.getTorderId().getOrderId().toString());
            maps.put("username", item.getTorderId().getUsername());
            maps.put("order_time", item.getTorderId().getOrderTime());
            maps.put("number", item.getTransport().getNumber());
            maps.put("total_cost", item.getTcostId().getTotalCost());
            return maps;
        }).collect(Collectors.toList());

//        // Title
//        int columns = Class.forName("com.heartsuit.springbootmybatis.oa.entity.Employee").getDeclaredFields().length;
//        writer.merge(columns - 1, "用户信息");

        //自定义标题别名
        writer.addHeaderAlias("order_id", "订单编号");
        writer.addHeaderAlias("username", "客户名称");
        writer.addHeaderAlias("order_time", "创建时间");
        writer.addHeaderAlias("number", "订单数量");
        writer.addHeaderAlias("total_cost", "应收总计");

        // Body
        writer.setColumnWidth(0, 30);
        writer.setColumnWidth(1, 30);
        writer.setColumnWidth(2, 30);
        writer.setColumnWidth(3, 30);
        writer.setColumnWidth(4, 30);
        writer.write(rows, true);

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
//        writer.write(sel, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("订单信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }


    //修改
    @PutMapping
   public Integer up( Integer id ,Integer state){
            int up = fin.up(id,state);
            return up;
    }



}
